#coding=utf-8

import smtplib
import traceback
import sys
import getpass
from email.mime.text import MIMEText
from email.header import Header 
   
if __name__ == '__main__':
	if len(sys.argv) < 3:
		print 'parameter error'
		print 'send-mail.py [filename] [receiver1] [receiver2] ...'
		sys.exit(-1)
	file = sys.argv[1]
	receivers = []
	for i in range(2,len(sys.argv)):
		receivers.append(sys.argv[i])
	
	host = 'smtp.sina.com'
	user = 'ihsc1990@sina.com'
	subject = 'automatically sent mail'
	
	content = open(file).read()
	msg = MIMEText(content,'text','utf-8')
	msg['Subject'] = Header(subject, 'utf-8') 
	
	passwd = getpass.getpass('sender password:')
	try:
		smtp = smtplib.SMTP(host)
		smtp.login(user, passwd)
		smtp.sendmail(user, receivers, msg.as_string())
		smtp.quit()
		print 'send email ok'
	except:
		print 'fail',traceback.format_exc()
